from PIL import Image
import numpy as np

# 读取图片
img = Image.open('C:/Users/86178/Desktop/数字图像/numpy_calculation_question_set-master/numpy_calculation_question_set-master/dog.jpg')

# 将图片转换为numpy数组
img_array = np.array(img)


# 定义函数来计算统计量
def calculate_stats(channel):
    # 计算最大值
    max_val = np.max(channel)

    # 计算最小值
    min_val = np.min(channel)

    # 计算平均值
    mean_val = np.mean(channel)

    # 计算标准差
    std_val = np.std(channel)

    # 计算分位数（使用numpy的percentile函数）
    percentile_25 = np.percentile(channel, 25)
    percentile_50 = np.percentile(channel, 50)
    percentile_75 = np.percentile(channel, 75)

    return max_val, min_val, mean_val, std_val, percentile_25, percentile_50, percentile_75


# 对每个通道（红、绿、蓝）执行计算
stats_red = calculate_stats(img_array[:, :, 0])
stats_green = calculate_stats(img_array[:, :, 1])
stats_blue = calculate_stats(img_array[:, :, 2])

# 打印结果
print("Red Channel:")
print(f"Max: {stats_red[0]}, Min: {stats_red[1]}, Mean: {stats_red[2]}, Std: {stats_red[3]}")
print(f"25%: {stats_red[4]}, 50%: {stats_red[5]}, 75%: {stats_red[6]}")

print("Green Channel:")
print(f"Max: {stats_green[0]}, Min: {stats_green[1]}, Mean: {stats_green[2]}, Std: {stats_green[3]}")
print(f"25%: {stats_green[4]}, 50%: {stats_green[5]}, 75%: {stats_green[6]}")

print("Blue Channel:")
print(f"Max: {stats_blue[0]}, Min: {stats_blue[1]}, Mean: {stats_blue[2]}, Std: {stats_blue[3]}")
print(f"25%: {stats_blue[4]}, 50%: {stats_blue[5]}, 75%: {stats_blue[6]}")